package com.itheima.day10_WZRY;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class Test {
    public static void main(String[] args) {
        ArrayList<hero> list = new ArrayList<>();
        String hand = null;
        //1、读取文件
        try(FileReader fis = new FileReader("E:\\soft\\file\\68期\\Java进阶\\课后作业\\day10\\hero.txt", Charset.forName("GBK"));
            BufferedReader bis = new BufferedReader(fis)) {


            //2、把数据作为对象放入集合
            hand = bis.readLine();
            String line;
            while ((line = bis.readLine()) != null) {
                String[] arr = line.split(",+");
                hero h = new hero(arr[0], Integer.parseInt(arr[1]), Integer.parseInt(arr[2]), Integer.parseInt(arr[3]));
                list.add(h);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //3、排序
        Collections.sort(list, new Comparator<hero>() {
            @Override
            public int compare(hero o1, hero o2) {
                if (o1.getNoviceFitness() == o2.getNoviceFitness()) {
                    return o1.getName().compareTo(o2.getName());
                } else {
                    return Double.compare(o2.getNoviceFitness(), o1.getNoviceFitness());
                }
            }
        });
        for (hero h : list) {
            System.out.println(h);
        }
        //4、写入文件
        try( FileWriter fw = new FileWriter("E:\\soft\\file\\68期\\Java进阶\\课后作业\\day10\\hero.txt", Charset.forName("GBK"));
            BufferedWriter bos = new BufferedWriter(fw)) {

            bos.write(hand + " 新手适应分值");
            bos.newLine();
            bos.flush();
            for (hero h : list) {
                bos.write(h.getName() + "," + h.getHp() + "," + h.getAttack() + "," + h.getResult() + "," + h.getNoviceFitness());
                bos.newLine();
                bos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
